import Vue from 'vue'
import Router from 'vue-router'

import store from '../store/modules/user'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import Index from "@/views"
/**
* Note: sub-menu only appear when route children.length >= 1
* Detail see: https://panjiachen.github.io/vue-element-admin-
site/guide/essentials/router-and-nav.html
*
* hidden: true                   if set true, item will not show in the sidebar(default is false)
* alwaysShow: true               if set true, will always show the root menu
*                                if not set alwaysShow, when item has more than one children route,
*                                it will becomes nested mode, otherwise not show the root menu
* redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
* name:'router-name'             the name is used by <keep-alive> (must set!!!)
* meta : {
roles: ['admin','editor']    control the page roles (you can set multiple roles)
title: 'title'               the name show in sidebar and breadcrumb (recommend set)
icon: 'svg-name'             the icon show in the sidebar
noCache: true                if set true, the page will no be cached(default is false)
affix: true                  if set true, the tag will affix in the tags-view
breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
}
*/

/**
* constantRoutes
* a base page that does not have permission requirements
* all roles can be accessed
*/
export const constantRoutes = [
  // {
  //   path: '/',
  //   component: Layout,
  //   redirect: '/home',
  //   children: [
  //     {
  //       path: 'home',
  //       component: () => import('@/views/home/index'),
  //       name: 'Dashboard',
  //       meta: { title: 'Home', icon: 'dashboard', affix: true }
  //     }
  //   ]
  // },
  // {
  //   path: '/redirect',
  //   component: Layout,
  //   hidden: true,
  //   children: [
  //     {
  //       path: '/redirect/:path(.*)',
  //       component: () => import('@/views/redirect/index')
  //     }
  //   ]
  // },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/test',
    component: () => import('@/views/thermalMap'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    // component: () => import('@/views/login/auth-redirect'),
    component: () => import('@/views/login.vue'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/platformManage/IEQHospitalPlatform',
    component: () => import('@/views/platformManage/components/IEQHospitalPlatform'),
    name: 'IEQHospitalPlatform',
    // hidden: true,
    meta: { title: '查看平台', icon: 'dashboard', affix: true }
  },
  {
    path: '/platformManage/IEQOfficePlatform',
    component: () => import('@/views/platformManage/components/IEQOfficePlatform'),
    name: 'IEQOfficePlatform',
    // hidden: true,
    meta: { title: '查看平台', icon: 'dashboard', affix: true }
  },
  {
    path: '/warningView/device',
    component: () => import('@/views/warningView/device/index'),
    hidden: true
  },
  {
    path: '/forgetPwd',
    component: () => import('@/views/forgetPwd/index'),
    hidden: true
  },
  {
    path: '/changePwd',
    component: () => import('@/views/forgetPwd/changePwd'),
    hidden: true
  },
  {
    path: '/',
    component: Index,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: () => import('@/views/home/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      },
      {
        path: '/inventoryManage',
        component: () => import('@/views/inventoryManage/index'),
        name: 'inventoryManage',
        meta: { title: '库存管理', icon: 'dashboard', affix: true }
      },
      {
        path: '/inventoryManage/user/index',
        component: () => import('@/views/inventoryManage/components/user/index'),
        name: 'userForm',
        // hidden: true,
        meta: { title: '用户列表', icon: 'dashboard', affix: true }
      },
      {
        path: '/deviceManage',
        component: () => import('@/views/deviceManage/index'),
        name: 'deviceManage',
        meta: { title: '设备管理', icon: 'dashboard', affix: true }
      },
      {
        path: '/deviceManage/detail',
        component: () => import('@/views/deviceManage/components/deviceView'),
        name: 'deviceDetail',
        // hidden: true,
        meta: { title: '设备详情', icon: 'dashboard', affix: true }
      },
      {
        path: '/deviceManage/deployDevice',
        component: () => import('@/views/deviceManage/components/deployDevice'),
        name: 'deployDevice',
        // hidden: true,
        meta: { title: '部署设备', icon: 'dashboard', affix: true }
      },
      {
        path: '/deviceManage/user/index',
        component: () => import('@/views/deviceManage/components/user/index'),
        name: 'userForm',
        // hidden: true,
        meta: { title: '用户列表', icon: 'dashboard', affix: true }
      },
      {
        path: '/deviceManage/IEQViewData',
        component: () => import('@/views/deviceManage/components/IEQViewData'),
        name: 'IEQViewData',
        // hidden: true,
        meta: { title: 'IEQ设备数据', icon: 'dashboard', affix: true }
      },
      {
        path: '/deviceManage/PMSViewData',
        component: () => import('@/views/deviceManage/components/PMSViewData'),
        name: 'PMSViewData',
        // hidden: true,
        meta: { title: 'PMS设备数据', icon: 'dashboard', affix: true }
      },
      {
        path: '/deviceManage/OPTViewData',
        component: () => import('@/views/deviceManage/components/OPTViewData'),
        name: 'OPTViewData',
        // hidden: true,
        meta: { title: 'OPT设备数据', icon: 'dashboard', affix: true }
      },
      {
        path: '/projectManage/viewProject',
        component: () => import('@/views/viewProject'),
        name: 'viewProject',
        // hidden: true,
        meta: { title: '查看项目', icon: 'dashboard', affix: true }
      },
      {
        path: '/projectManage',
        component: () => import('@/views/projectManage/index'),
        name: 'projectManage',
        meta: { title: '项目/建筑管理', icon: 'dashboard', affix: true },
      },
      {
        path: '/projectManage/EditForm',
        component: () => import('@/views/projectManage/components/EditForm'),
        name: 'EditForm',
        meta: { title: '编辑项目', icon: 'dashboard', affix: true },
      },
      {
        path: '/projectManage/projectDeviceDeploy',
        component: () => import('@/views/viewProject/components/device/deployDevice'),
        name: 'projectDeviceDeploy',
        meta: { title: '部署设备', icon: 'dashboard', affix: true }
      },
      {
        path: '/platformManage',
        component: () => import('@/views/platformManage/index'),
        name: 'platformManage',
        meta: { title: '平台管理', icon: 'dashboard', affix: true }
      },
      {
        path: '/platformManage/platformView',
        component: () => import('@/views/platformManage/components/platformView'),
        name: 'platformView',
        // hidden: true,
        meta: { title: '平台详情', icon: 'dashboard', affix: true }
      },
      {
        path: '/platformManage/user/index',
        component: () => import('@/views/platformManage/user/index'),
        name: 'platformUserForm',
        // hidden: true,
        meta: { title: '用户列表', icon: 'dashboard', affix: true }
      },
      {
        path: '/platformManage/device',
        component: () => import('@/views/platformManage/device/deviceList'),
        name: 'deviceListManage',
        meta: { title: '设备管理', icon: 'dashboard', affix: true },
      },
      {
        path: '/userManage',
        component: () => import('@/views/userManage/index'),
        name: 'userManage',
        meta: { title: '用户管理', icon: 'dashboard', affix: true }
      },
      {
        path: '/sceneManage',
        component: () => import('@/views/sceneManage/index'),
        name: 'sceneManage',
        meta: { title: '场图管理', icon: 'dashboard', affix: true }
      },
      {
        path: '/sceneManage/user/index',
        component: () => import('@/views/sceneManage/user/index'),
        name: 'sceneUserForm',
        // hidden: true,
        meta: { title: '用户列表', icon: 'dashboard', affix: true }
      },
      {
        path: '/userView',
        component: () => import('@/views/userManage/components/userView'),
        name: 'userView',
        meta: { title: '用户详情', icon: 'dashboard', affix: true }
      },
      {
        path: '/addUser',
        component: () => import('@/views/userManage/components/editForm'),
        name: 'addUser',
        meta: { title: '添加用户', icon: 'dashboard', affix: true }
      },
      {
        path: '/userManage/device',
        component: () => import('@/views/userManage/device/deviceList'),
        name: 'deviceList',
        meta: { title: '查看设备', icon: 'dashboard', affix: true },
      }
    ]
  },
  {
    path: '/platformDetail',
    redirect: '/platformDetail/home',
    component: () => import('@/views/platformDetail/index'),
    children: [
      {
        path: '/platformDetail/home',
        component: () => import('@/views/platformDetail/home/index'),
        name: 'env',
        meta: { title: '环境总况', icon: 'dashboard', affix: true }
      },
      {
        path: '/platformDetail/deviceManage',
        component: () => import('@/views/platformDetail/deviceManage/index'),
        name: 'pltDeviceManage',
        meta: { title: '设备管理', icon: 'dashboard', affix: true }
      },
      {
        path: '/platformDetail/deviceManage/IEQViewData',
        component: () => import('@/views/deviceManage/components/IEQViewData'),
        name: 'IEQViewData',
        // hidden: true,
        meta: { title: 'IEQ设备数据', icon: 'dashboard', affix: true }
      },
      {
        path: '/platformDetail/deviceManage/PMSViewData',
        component: () => import('@/views/deviceManage/components/PMSViewData'),
        name: 'PMSViewData',
        // hidden: true,
        meta: { title: 'PMS设备数据', icon: 'dashboard', affix: true }
      },
      {
        path: '/platformDetail/deviceManage/OPTViewData',
        component: () => import('@/views/deviceManage/components/OPTViewData'),
        name: 'OPTViewData',
        // hidden: true,
        meta: { title: 'OPT设备数据', icon: 'dashboard', affix: true }
      },
    ]

  },
  {
    path: '/scenePlatformDetail',
    redirect: '/scenePlatformDetail/home',
    component: () => import('@/views/scenePlatformDetail/index'),
    children: [
      {
        path: '/scenePlatformDetail/home',
        component: () => import('@/views/scenePlatformDetail/home/index'),
        name: 'env',
        meta: { title: '环境总况', icon: 'dashboard', affix: true }
      },
      {
        path: '/scenePlatformDetail/dataSearch',
        component: () => import('@/views/scenePlatformDetail/dataSearch/index'),
        name: 'dataSearch',
        meta: { title: '数据查询', icon: 'dashboard', affix: true }
      },
      {
        path: '/scenePlatformDetail/deviceManage',
        component: () => import('@/views/scenePlatformDetail/deviceManage/index'),
        meta: { title: '设备管理', icon: 'dashboard', affix: true }
      },
      {
        path: '/scenePlatformDetail/robotManage',
        component: () => import('@/views/scenePlatformDetail/robotManage/index'),
        meta: { title: '机器人管理', icon: 'dashboard', affix: true }
      },
      {
        path: '/scenePlatformDetail/control',
        component: () => import('@/views/scenePlatformDetail/control/index'),
        meta: { title: '管理控制', icon: 'dashboard', affix: true }
      },
    ]

  },
  {
    path: '/OPTPlatformDetail',
    redirect: '/OPTPlatformDetail/home',
    component: () => import('@/views/OPTPlatformDetail/index'),
    children: [
      {
        path: '/OPTPlatformDetail/home',
        component: () => import('@/views/OPTPlatformDetail/home/index'),
        name: 'env',
        meta: { title: '环境总况', icon: 'dashboard', affix: true }
      },
      {
        path: '/OPTPlatformDetail/deviceManage',
        component: () => import('@/views/OPTPlatformDetail/deviceManage/index'),
        name: 'pltDeviceManage',
        meta: { title: '设备管理', icon: 'dashboard', affix: true }
      },
      {
        path: '/OPTPlatformDetail/deviceManage/IEQViewData',
        component: () => import('@/views/deviceManage/components/IEQViewData'),
        name: 'IEQViewData',
        // hidden: true,
        meta: { title: 'IEQ设备数据', icon: 'dashboard', affix: true }
      },
      {
        path: '/OPTPlatformDetail/deviceManage/PMSViewData',
        component: () => import('@/views/deviceManage/components/PMSViewData'),
        name: 'PMSViewData',
        // hidden: true,
        meta: { title: 'PMS设备数据', icon: 'dashboard', affix: true }
      },
      {
        path: '/OPTPlatformDetail/deviceManage/OPTViewData',
        component: () => import('@/views/deviceManage/components/OPTViewData'),
        name: 'OPTViewData',
        // hidden: true,
        meta: { title: 'OPT设备数据', icon: 'dashboard', affix: true }
      },
    ]

  },
  {
    path: '/PMSPlatformDetail',
    redirect: '/PMSPlatformDetail/home',
    component: () => import('@/views/PMSPlatformDetail/index'),
    children: [
      {
        path: '/PMSPlatformDetail/home',
        component: () => import('@/views/PMSPlatformDetail/home/index'),
        name: 'env',
        meta: { title: '环境总况', icon: 'dashboard', affix: true }
      },
      {
        path: '/PMSPlatformDetail/deviceManage',
        component: () => import('@/views/PMSPlatformDetail/deviceManage/index'),
        name: 'pltDeviceManage',
        meta: { title: '设备管理', icon: 'dashboard', affix: true }
      },
      {
        path: '/PMSPlatformDetail/deviceManage/IEQViewData',
        component: () => import('@/views/deviceManage/components/IEQViewData'),
        name: 'IEQViewData',
        // hidden: true,
        meta: { title: 'IEQ设备数据', icon: 'dashboard', affix: true }
      },
      {
        path: '/PMSPlatformDetail/deviceManage/PMSViewData',
        component: () => import('@/views/deviceManage/components/PMSViewData'),
        name: 'PMSViewData',
        // hidden: true,
        meta: { title: 'PMS设备数据', icon: 'dashboard', affix: true }
      },
      {
        path: '/PMSPlatformDetail/deviceManage/OPTViewData',
        component: () => import('@/views/deviceManage/components/OPTViewData'),
        name: 'OPTViewData',
        // hidden: true,
        meta: { title: 'OPT设备数据', icon: 'dashboard', affix: true }
      },
    ]

  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()
const whiteList = ['/login', '/register', '/forgetPwd', '/changePwd'];
//console.log(store.state.isLogin);
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem("isLogin");

  if (whiteList.indexOf(to.path) == -1) {  // 判断该路由是否需要登录权限
    if (isLogin) {  // 判断当前的token是否存在
      next();
    }
    else {
      next({
        path: '/login'
      })
    }
  }
  else {
    next();
  }
});

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment- 357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
